package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.shuqi.database.model.BookInfo;
import com.shuqi.model.bean.MoreRecommendInfo;
import com.shuqi.model.parse.HandlerBase;

/**
 * 更多推荐解析器
 * 
 * @author zhuchen
 * 
 */
public class MoreRecommengParser extends HandlerBase {

    private BookInfo bookInfo;
    private MoreRecommendInfo moreRecommendInfo;
    private List<BookInfo> bookInfoList;
    private int inWhich;
    private static final int BOOKNAME = 1;
    private static final int AUTHOR = 2;
    private static final int BOOKTYPE = 3;
    private static final int BOOKDESCRIPTION = 4;

    @Override
    public Object getParsedData() {
        // TODO Auto-generated method stub
        return moreRecommendInfo;
    }

    @Override
    public void startDocument() throws SAXException {
        bookInfoList = new ArrayList<BookInfo>();
        moreRecommendInfo = new MoreRecommendInfo();
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        if (localName.equals("Books")) {
            moreRecommendInfo.setColumnId(isNull(attributes, "columnId"));
            moreRecommendInfo.setTitle(isNull(attributes, "title"));
            moreRecommendInfo.setPageCount(isNull(attributes, "pageCount"));
            moreRecommendInfo.setTotalCount(isNull(attributes, "totalCount"));
            moreRecommendInfo.setBookInfo(bookInfoList);
        } else if (localName.equals("Book")) {
            bookInfo = new BookInfo();
            bookInfo.setBookId(isNull(attributes, "bookId"));
            bookInfo.setBookCoverImgUrl(isNull(attributes, "imgUrl"));
            bookInfo.setBookHideState(isNull(attributes, "hide"));
            String coverState = isNull(attributes, "cr_cover_isopen");
            if(coverState != null){
                try{
                    bookInfo.setCoverHideState(Integer.valueOf(coverState));
                }catch(Exception e){
                    
                }
            }
            moreRecommendInfo.getBookInfo().add(bookInfo);
        } else if (localName.equals("BookName")) {
            this.inWhich = BOOKNAME;
        } else if (localName.equals("Author")) {
            this.inWhich = AUTHOR;
        } else if (localName.equals("BookType")) {
            this.inWhich = BOOKTYPE;
        } else if (localName.equals("Description")) {
            this.inWhich = BOOKDESCRIPTION;
        } else {
            this.inWhich = 0;
        }

        super.startElement(uri, localName, qName, attributes);
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String value = new String(ch, start, length);
        if (value != null && !value.trim().equals("")) {
            switch (this.inWhich) {
                case BOOKNAME:
                    this.bookInfo.setBookName((bookInfo.getBookName() == null ? "" : bookInfo
                            .getBookName()) + value);
                    break;
                case AUTHOR:
                    this.bookInfo.setBookAuthorName((bookInfo.getBookAuthorName() == null ? "" : bookInfo
                            .getBookAuthorName()) + value);
                    break;
                case BOOKTYPE:
                    this.bookInfo.setBookType((bookInfo.getBookType() == null ? "" : bookInfo
                            .getBookType()) + value);
                    break;
                case BOOKDESCRIPTION:
                    this.bookInfo.setBookIntro((bookInfo.getBookIntro() == null ? "" : bookInfo.getBookIntro())
                            + value);
                    break;
            }
        }
    }

    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
    }
}
